import random
import re
import os
import string
import sys
from shutil import copyfile


class Img:
    def __init__(self):

        self.source = input("Enter source file with full path: \n")
        self.reg = r'\.\w+'
        self.regcompiled = re.compile(self.reg)
        self.source = self.source[1:-1]
        if re.findall(self.regcompiled, self.source):
            print(re.findall(self.regcompiled, self.source)[0][1:])
            self.filetype = re.findall(self.regcompiled, self.source)[0][1:]
        else:
            pass

        # 随机文件名
        seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&_+=-"
        sa = []
        for i in range(32):
            sa.append(random.choice(seed))
        salt = ''.join(sa)

        self.target = './img/' + salt + '.' + self.filetype


    def copyfiletoimg(self):
        try:
            copyfile(self.source, self.target)
        except IOError as e:
            print("Unable to copy file. %s" % e)
            exit(1)
        except:
            print("Unexpected error:", sys.exc_info())
            exit(1)

if __name__ == '__main__':

    a = Img()
    a.copyfiletoimg()

    # 第一种方法
    seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&_+=-"
    sa = []
    for i in range(32):
        sa.append(random.choice(seed))
    salt = ''.join(sa)

    print(salt)

    # adding exception handling
    # try:
    #    copyfile(source, target)
    # except IOError as e:
    #    print("Unable to copy file. %s" % e)
